﻿using System;

using zijian666.Abstractions;
using zijian666.Convertor.Base;
using zijian666.Core;
using zijian666.ConvertExtensions;

namespace zijian666.Convertor.Extra;

/// <summary>
/// <seealso cref="Version"/> 转换器
/// </summary>
public class VersionConvertor : AllowNullConvertor<Version>
                              , IFrom<string, Version>
                              , IFrom<object, Version>
{
    /// <inheritdoc/>
    public ConvertResult<Version> From(IConvertContext context, string input)
    {
        if (Version.TryParse(input, out var result))
        {
            return new(result);
        }
        return context.ConvertFail(this, input);
    }

    /// <inheritdoc/>
    public ConvertResult<Version> From(IConvertContext context, object input) => From(context, input.ToString());
}
